package org.compiere.model;

import java.sql.ResultSet;
import java.util.Properties;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;

public class Mhrmsalaryrange extends X_hrm_salaryrange {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7783539487406114945L;
	
	// Error message
	private StringBuilder errmsg = new StringBuilder();

	public Mhrmsalaryrange(Properties ctx, int hrm_salaryrange_ID,
			String trxName) {
		super(ctx, hrm_salaryrange_ID, trxName);
	}

	public Mhrmsalaryrange(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}

	private void updPeriod(PO po){
		// Upd period StartDate <-> EndDate
		Util.updPeriod(po, "HR_Department_ID,HR_job_ID");
	}
		
	
	@Override
	public boolean save() {
	
		if(!super.save())
			throw new AdempiereException("");
		updPeriod(this);
		
		return true;
	}
	
	
	@Override
	protected boolean afterSave(boolean newRecord, boolean success) {

		boolean result =  super.afterSave(newRecord, success);
		updPeriod(this);
		
		return result;
	}
	
	@Override
	public boolean delete(boolean force) {
		
		X_hrm_salaryrange hrmsa = new X_hrm_salaryrange(this.getCtx(), get_ID(), this.get_TrxName());
		
		boolean de = super.delete(force);
		// copy Values
		updPeriod(hrmsa);
		
		return de;
	}
	
	@Override
	protected boolean beforeSave(boolean newRecord) {
		
		//
		if(newRecord && Util.isOverlay(this, "HR_Department_ID,HR_Job_ID"))
			throw new AdempiereException(Msg.translate(Env.getCtx(), "OverlayDates"));
		
		if(gethrm_salarymin() == 0) 
			errmsg.append(Util.requiredField(COLUMNNAME_hrm_salarymin)).append("\n");
		if(gethrm_salarymax() == 0) 
			errmsg.append(Util.requiredField(COLUMNNAME_hrm_salarymax)).append("\n");
		else if(gethrm_salarymax() < gethrm_salarymin())
			errmsg.append("Minimum salary > Maximum salary").append("\n");
			
		Util.checkErrMsg(errmsg);
 
		return true;
	}
	
}
